package com.ming.IOStudy;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * @Auther: tangC
 * @Date: 2020/7/30 10:36
 * @Description:
 */
public class InAndOut {


    public void test() throws Exception{
        File file = new File("C://1.txt");
        File file2 = new File("C://2.txt");
        InputStream inputStream = new  FileInputStream(file);
        OutputStream outputStream = new FileOutputStream(file2);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        byte[] buf = new byte[1024];
        int size = 0;
        while ((size = bufferedInputStream.read(buf,0,1024))!= -1){
            bufferedOutputStream.write(buf,0,size);
        }
        bufferedOutputStream.flush();
    }

    public void test2(){
        FileChannel in = null;
        FileChannel out = null;
        File file = new File("C://1.txt");
        File file2 = new File("C://3.txt");
        try {
            in = new FileInputStream(file).getChannel();
            out = new FileOutputStream(file2).getChannel();
            out.transferFrom(in,0,in.size());
        }catch (Exception e){

        }

    }

    public static void main(String[] args) {
        InAndOut inAndOut = new InAndOut();
        try {
            inAndOut.test2();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
